package at.fhj.itm10.ticketmaster.services.impl;

import java.util.List;

import org.apache.log4j.Logger;

import at.fhj.itm10.ticketmaster.dao.DAOException;
import at.fhj.itm10.ticketmaster.dao.OrderDAO;
import at.fhj.itm10.ticketmaster.entities.Order;
import at.fhj.itm10.ticketmaster.helper.Validator;
import at.fhj.itm10.ticketmaster.services.OrderService;
import at.fhj.itm10.ticketmaster.services.ServiceException;

/**
 * {@inheritDoc}
 * 
 * @author Kevin Reinmueller
 */
public class OrderServiceImpl extends AbstractService implements OrderService {

	private Logger logger = Logger.getLogger(OrderServiceImpl.class);
	private OrderDAO dao;

	public OrderDAO getDao() {
		return dao;
	}
	
	public void setDao(OrderDAO orderDao) {
		this.dao = orderDao;
	}

	/**
	 * Insert Order if not null 
	 * If Order ID zero generate new
	 */
	public void insertOrder(Order order) {
		logger.debug("insertOrder()");
		logger.debug("oderID= " + order.getId());
		logger.debug("custID= " + order.getFk_customer_id());
		Validator.notNullOrThrow(order, "order is null");
		Validator.zeroOrThrow(order.getId(), "id for new order must be zero - will be autofilled");

		try {
			begin();
			getDao().insert(order);
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on inserting data", e);
		} finally {
			closeConnection();
		}
	}

	/**
	 * Update Order if not Null
	 */
	public void updateOrder(Order order) {
		logger.debug("updateOrder()");
		
		Validator.notNullOrThrow(order, "order is null");
		
		try {
			begin();
			getDao().update(order);
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on updating data", e);
		} finally {
			closeConnection();
		}
	}

	/**
	 * Delete Order if not null
	 */
	public void deleteOrder(Order order) {
		logger.debug("deleteOrder()");
		
		Validator.notNullOrThrow(order, "order is null");

		try {
			begin();
			getDao().delete(order);
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on deleting data", e);
		} finally {
			closeConnection();
		}
	}

	/**
	 * search for Order by id
	 */
	public Order findById(long id) {
		logger.debug("findById()");

		Order order = null;
		try {
			begin();
			order = getDao().findById(id);
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on retrieving data", e);
		} finally {
			closeConnection();
		}
		
		return order;
	}
	
	/**
	 * return all available orders
	 */
	public List<Order> findAll() {
		logger.debug("findAll()");

		List<Order> orders = null;
		try {
			begin();
			orders = getDao().findAll();
			commit();
		} catch (DAOException e) {
			rollback();

			throw new ServiceException("Error on retrieving data", e);
		} finally {
			closeConnection();
		}
		
		return orders;
	}
}
